package com.ruoyi.labour.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.labour.domain.LabourCustomer;
import com.ruoyi.labour.domain.vo.PageVo;
import com.ruoyi.labour.domain.vo.UpdateCustomerPhoneVo;
import com.ruoyi.labour.enums.CustomerPhoneStatus;
import com.ruoyi.labour.mapper.CustomerPhoneMapper;
import com.ruoyi.labour.service.CustomerPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: CustomerPhoneServiceImpl
 * @Description:
 * @Author: Awen
 * @Date: 2023/5/15 11:26
 */
@Service
public class CustomerPhoneServiceImpl implements CustomerPhoneService {

    @Autowired
    private CustomerPhoneMapper customerPhoneMapper;

    /**
     * 添加热线电话
     *
     * @param phone
     * @return
     */
    @Override
    public R addCustomerPhone(String phone) {
        int i = customerPhoneMapper.checkCustomerPhoneExist(phone);
        if (i > 0) {
            return R.fail("该热线电话已存在");
        } else {
            customerPhoneMapper.addCustomerPhone(phone);
            return R.ok("添加成功");
        }
    }

    /**
     * 查询热线电话
     *
     * @param pageVo
     * @return
     */
    @Override
    public R selectCustomerPhone(PageVo pageVo) {

        PageHelper.startPage(pageVo.getPage(), pageVo.getSize());
        List<LabourCustomer> labourCustomers = customerPhoneMapper.selectCustomerPhone();
        PageInfo pageInfo = new PageInfo(labourCustomers);

        return R.ok(pageInfo);
    }

    /**
     * 删除热线电话
     *
     * @param id
     * @return
     */
    @Override
    public R deleteCustomerPhone(Integer id) {
        int i = customerPhoneMapper.checkCustomerPhoneExistById(id);
        if (i > 0) {
            customerPhoneMapper.deleteCustomerPhone(id);
            return R.ok("删除成功");
        } else {
            return R.fail("该热线电话不存在");
        }
    }

    /**
     * 修改热线电话可用状态
     *
     * @param state
     * @param id
     * @return
     */
    @Override
    public R updateCustomerPhoneState(Integer state, Integer id) {

        int i = customerPhoneMapper.checkCustomerPhoneExistById(id);
        if (i > 0) {
            if ("[0][1]".contains("[" + state + "]")) {
                customerPhoneMapper.updateCustomerPhoneState(state, id);
                return R.ok("修改成功，当前热线电话状态为" + CustomerPhoneStatus.parse(state));
            } else {
                return R.fail("热线电话状态值不存在");
            }
        } else {
            return R.fail("该热线电话不存在");
        }
    }

    /**
     * 修改热线电话
     *
     * @param updateCustomerPhoneVo
     * @return
     */
    @Override
    public R updateCustomerPhone(UpdateCustomerPhoneVo updateCustomerPhoneVo) {
        int i = customerPhoneMapper.checkCustomerPhoneExistById(updateCustomerPhoneVo.getId());
        int j = customerPhoneMapper.checkCustomerPhoneExist(updateCustomerPhoneVo.getCustomerPhone());
        if (i > 0) {
            if (j > 0 ){
                return R.fail("该热线电话已存在");
            }
            customerPhoneMapper.updateCustomerPhone(updateCustomerPhoneVo);

            return R.ok("修改成功");
        } else {
            return R.fail("该热线电话不存在");
        }
    }


}
